﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

// https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x804 上介绍了“空白页”项模板

namespace Acoris.UWP
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class MainPage : Page
    {
        
        public MainPage() {
            this.InitializeComponent();
            var thisAssembly = Assembly.Load(new AssemblyName("Acoris.UWP"));
            var demoAttributes = thisAssembly.DefinedTypes.Select((type)=>type.GetCustomAttribute<DemoAttribute>());

            foreach (var demo in demoAttributes) {
                if (demo == null) {
                    continue;
                }
                var ctor = demo.Namespace.GetConstructor(new Type[0]);
                var instance = ctor.Invoke(null);
                DemosList.Items.Add(demo);
            }
        }

        private void SelectionChanged(object sender, SelectionChangedEventArgs e) {
            var demo = DemosList.SelectedItem as DemoAttribute;
            var ctor = demo.Namespace.GetConstructor(new Type[0]);
            var instance = ctor.Invoke(null);
            var element = (FrameworkElement)instance;
            element.Width = content.Width;
            element.Height = content.Height;
            content.Content = instance;
        }

        public bool IsOpen { get; set; } = true;
    }   
}
